<template>
  <div class="container">
    <Breadcrumb :items="[
      'menu.pay.internetofthings',
      'menu.pay.internetofthings.carpark',
      'menu.internetofthings.carpark.releaselog',
    ]" />
    <a-card class="general-card" :title="$t('menu.internetofthings.carpark.releaselog')">
      <a-row>
        <a-col :flex="1">
          <a-form :model="formModel" :label-col-props="{ span: 6 }" :wrapper-col-props="{ span: 18 }" label-align="left">
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item label="停车场" field="carParkId" required>
                  <a-select :loading="selectLoading" placeholder="请输入" allow-search allow-clear
                    @search="handleSelectSearch" :filter-option="false" v-model="formModel.carParkId" @change="parkChange">
                    <a-option v-for="item of parkList" :value="item.id">{{
                      item.name
                    }}</a-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="入口" field="inCheckPointId">
                  <a-select
                      v-model="formModel.inCheckPointId"
                      placeholder="请选择"
                      allow-clear
                  >
                    <a-option
                        :value="item.id"
                        v-for="item in inCheckPointList"
                        :key="item.id"
                    >{{ item.name }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="出口" field="outCheckPointId">
                  <a-select
                      v-model="formModel.outCheckPointId"
                      placeholder="请选择"
                      allow-clear
                  >
                    <a-option
                        :value="item.id"
                        v-for="item in outCheckPointList"
                        :key="item.id"
                    >{{ item.name }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="申请人" field="uid">
                  <a-select
                      v-model="formModel.uid"
                      :style="{ width: '320px' }"
                      :loading="loading"
                      placeholder="请输入用户手机号"
                      allow-search
                      :filter-option="false"
                      allow-clear
                      @search="handleSearch"
                  >
                    <a-option
                        v-for="item of userList"
                        :key="item?.id"
                        :value="item?.id"
                    >{{ item.userName }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item
                    label="状态"
                    field="status"
                    validate-trigger="input"
                >
                  <a-select
                      v-model="formModel.status"
                      placeholder="请选择"
                      allow-clear
                  >
                    <a-option
                        :value="item.value"
                        v-for="item in statusTypeArr"
                        :key="item.value"
                    >{{ item.label }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item
                    label="信号来源"
                    field="signalSource"
                    validate-trigger="input"
                >
                  <a-select
                      v-model="formModel.signalSource"
                      placeholder="请选择"
                      allow-clear
                  >
                    <a-option
                        :value="item.value"
                        v-for="item in signalSourceTypeArr"
                        :key="item.value"
                    >{{ item.label }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 40px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              查询
            </a-button>
            <!-- <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              {{ $t('searchTable.form.reset') }}
            </a-button> -->
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-row style="margin-bottom: 16px">
        <a-col :span="12">
          <a-space>
<!--            <a-button type="primary" @click="handleAdd">-->
<!--              <template #icon>-->
<!--                <icon-plus />-->
<!--              </template>-->
<!--              {{ $t('searchTable.operation.create') }}-->
<!--            </a-button>-->
<!--            <a-button @click="showPackageImport">-->
<!--              {{ $t('searchTable.operation.import') }}-->
<!--            </a-button>-->
          </a-space>
        </a-col>
        <a-col :span="12" style="display: flex; align-items: center; justify-content: end">
<!--          <a-button>-->
<!--            <template #icon>-->
<!--              <icon-download />-->
<!--            </template>-->
<!--            {{ $t('searchTable.operation.download') }}-->
<!--          </a-button>-->
        </a-col>
      </a-row>
      <a-table row-key="id" :columns="columns" :data="state.list" :style="{ height: '700px' }" :pagination="pagination"
        :loading="loading" @page-change="pageChange">
        <template #index="{ rowIndex }">
          {{ rowIndex + 1 + (pagination.current - 1) * pagination.pageSize }}
        </template>
        <template #operations="{ record }">
          <a-space>
            <a-popconfirm content="您确定删除吗？" @ok="onDelete(record.id)">
              <a-button type="primary" status="danger" size="mini">删除</a-button>
            </a-popconfirm>
<!--            <a-button type="primary" size="mini" @click="handleEdit(record)">编辑</a-button>-->
          </a-space>
        </template>
      </a-table>
    </a-card>
    <editAdd ref="editAddRef" @fetch-data="fetchData" />
    <packageImport ref="packageImportRef" @fetch-data="fetchData" />
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import {
  deleteReleaseLog,
  cfCarParkSearch,
  getReleaseLogListByQuery
} from '@/api/internetOfThings';
import editAdd from './editAdd.vue';
import packageImport from './packageImport.vue';
import {parseTime} from "@/utils";
import {cfCheckPointGet} from "@/api/park";
import {cfUserQuicklySearchUser} from "@/api/user";

const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0,
});
const loading = ref(false);
const selectLoading = ref(false);

const editAddRef = ref();
const packageImportRef = ref();
const inCheckPointList = ref([]);
const outCheckPointList = ref([]);
const userList = ref([]);

//状态(0-待执行/1-执行成功/2-执行失败)
const statusTypeArr = [
  { value: 0, label: '待执行' },
  { value: 1, label: '执行成功' },
  { value: 2, label: '执行失败' },
];
//信号来源(0-遥控/1-软件)
const signalSourceTypeArr = [
  { value: 0, label: '遥控' },
  { value: 1, label: '软件' },
];

const columns = [
  {
    title: '停车场名称',
    dataIndex: 'carParkName',
  },
  {
    title: '通道名称',
    dataIndex: 'checkPointName',
  },
  {
    title: '申请人',
    dataIndex: 'userName',
  },
  {
    title: '设备',
    dataIndex: 'deviceSerialNumber',
  },
  {
    title: '串口',
    dataIndex: 'serialPortNumber',
  },
  {
    title: '申请时间',
    dataIndex: 'newApplicationTime',
  },
  {
    title: '申请原因',
    dataIndex: 'applicationReason',
  },
  {
    title: '状态',
    dataIndex: 'newStatus',
  },
  {
    title: '信号来源',
    dataIndex: 'newSignalSource',
  },
  {
    title: '操作',
    dataIndex: 'operations',
    slotName: 'operations',
  },
];
const parkList = ref([]);
const state = reactive({
  list: [],
});
const generateFormModel = () => {
  return {
    plateNumber: '',
    carParkId: '',
    inCheckPointId: '',
    outCheckPointId: '',
    uid: '',
    status: '',
    signalSource: '',
  };
};
const formModel = ref(generateFormModel());

onMounted(() => {
  fetchData();
  handleSelectSearch(null);
});
const showPackageImport = () => {
  packageImportRef.value.handleClick();
}
const handleSelectSearch = async (value: any) => {
    selectLoading.value = true;
    const { data, code } = await cfCarParkSearch({
      params: {
        name: value,
        page: 1,
        size: 200,
      },
    });
    selectLoading.value = false;
    if (code === 10002) {
      parkList.value = data;
    } else {
      parkList.value = [];
    }
};
const handleSearch = async (value: any) => {
  const { data, code } = await cfUserQuicklySearchUser({
    params: {
      phone: value,
      page: 1,
      size: 20,
    },
  });
  if (code === 10002) {
    userList.value = data;
  } else {
    userList.value = [];
  }
};
const pageChange = (e: number) => {
  pagination.current = e;
  fetchData();
};
const parkChange = async (value: any) => {
  if (value) {
    selectLoading.value = true;
    const { data, code } = await cfCheckPointGet({
      params: {
        areaId: value,
        page: 1,
        size: 200,
      },
    });
    selectLoading.value = false;
    if (code === 10002) {
      inCheckPointList.value = [];
      outCheckPointList.value = [];
      for(const checkPoint of data){
        if(checkPoint.features=='in'){
          inCheckPointList.value.push(checkPoint);
        }else{
          outCheckPointList.value.push(checkPoint);
        }
      }
    } else {
      inCheckPointList.value = [];
      outCheckPointList.value = [];
    }
  } else {
    inCheckPointList.value = [];
    outCheckPointList.value = [];
  }
};
const handleAdd = () => {
  editAddRef.value.handleClick();
};
const handleEdit = (e: any) => {
  editAddRef.value.handleClick(e);
};
const search = () => {
  pagination.current = 1;
  fetchData();
};
const onDelete = async (id: string) => {
  await deleteReleaseLog({ params: { id } });
  fetchData();
};
const checkParams = (obj: any) => {
  // eslint-disable-next-line no-restricted-syntax, guard-for-in
  for (const i in obj) {
    if (!obj[i]) {
      if (obj[i] !== 0) {
        obj[i] = null;
      }
    }
  }
  return obj;
};
const fetchData = async () => {
  loading.value = true;
  // @ts-ignore
  const { data, code, total } = await getReleaseLogListByQuery({
    params: checkParams({
      page: pagination.current,
      size: pagination.pageSize,
      plateNumber: formModel.value.plateNumber,
      carParkId: formModel.value.carParkId,
    }),
  });
  loading.value = false;
  if (code === 10002) {
    // @ts-ignore
    if (total) pagination.total = total;
    for (const i of data) {
      i.newApplicationTime = parseTime(i.applicationTime) || '-';
      switch(i.status){
        case 0:
          i.newStatus = '待执行';
          break;
        case 1:
          i.newStatus = '执行成功';
          break;
        case 2:
          i.newStatus = '执行失败';
          break;
      }
      switch(i.signalSource){
        case 0:
          i.newSignalSource = '遥控';
          break;
        case 1:
          i.newSignalSource = '软件';
          break;
      }
    }
    state.list = data;
  } else {
    state.list = [];
  }
  // console.log(data, code, 999);
};
</script>

<script lang="ts">
export default {
  name: 'SearchTable',
};
</script>

<style scoped lang="less">
.container {
  padding: 0 20px 20px 20px;
}
</style>
